package com.ruoyi.framework.mybatisplus.util;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.RowConstructor;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.statement.select.SetOperationList;
import net.sf.jsqlparser.statement.values.ValuesStatement;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yql
 * @describe InterceptorUtils
 * @date 2023/3/13 17:05
 */

public class InterceptorUtils {
    public static List<ExpressionList> getSetOperationList(Insert statement) {
        ArrayList<ExpressionList> expressionList = new ArrayList<>();
        for (SelectBody body : statement.getSelect().getSelectBody(SetOperationList.class).getSelects()) {
            if (body instanceof ValuesStatement && ((ValuesStatement) body).getExpressions() instanceof ExpressionList) {
                for (Expression expression : ((ExpressionList) ((ValuesStatement) body).getExpressions()).getExpressions()) {
                    if (expression instanceof RowConstructor) {
                        expressionList.add(((RowConstructor) expression).getExprList());
                    }
                }
            }
        }
        return expressionList;
    }
}
